<template>
    <div>
        <h1>资讯编辑中心</h1>
        <el-input type="text" placeholder="请输入标题" style="margin-bottom : 20px;width:300px" v-model="title"></el-input>
        <div style="border: 1px solid #ccc">
            <Toolbar style="border-bottom: 1px solid #ccc" :editor="editorRef" :defaultConfig="toolbarConfig"
                :mode="mode" />
            <Editor style="height: 500px; overflow-y: hidden;" v-model="valueHtml" :defaultConfig="editorConfig"
                :mode="mode" @onCreated="handleCreated" />
        </div>
        <div class="a">
            <el-button type="primary" @click="handleClick">提交到审核中心</el-button>
        </div>

    </div>
</template>

<script lang="ts" setup>
import '@wangeditor/editor/dist/css/style.css' // 引入 css

import { onBeforeUnmount, ref, shallowRef, onMounted } from 'vue'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue';
import { responseCallback } from '../../../../utils';
import { announcementAddApi } from '../../../../api/a';


const editorRef = shallowRef()

const title = ref("");
const valueHtml = ref('')

const toolbarConfig = {}
const editorConfig = {
    placeholder: '请输入内容...',
    MENU_CONF: {
        uploadImage: {
            server: '/api/upload/image',
        }
    },
}

// 组件销毁时，也及时销毁编辑器
onBeforeUnmount(() => {
    const editor = editorRef.value
    if (editor == null) return
    editor.destroy()
})
const mode = 'default'; // 或 'simple'
const handleCreated = (editor) => {
    editorRef.value = editor // 记录 editor 实例，重要！
}

const handleClick = () => {
    responseCallback({
        promise: announcementAddApi({
            content: valueHtml.value,
            title: title.value
        }),
        successMessage: "提交审核成功，请联系审核员进行审核"
    })
}


</script>

<style lang="less" scoped>
h1 {
    color: #333;
    text-align: center;
    font-size: 28px;
    margin-bottom: 2rem;
}

img {
    display: inline-block;
}

.a {
    display: flex;
    justify-content: flex-end;

    margin-top: 1rem;

    .el-button {
        width: auto;
    }
}
</style>